#include "collect.h"
#include "Ai.hpp"
#include "world.hpp"
#include <iostream>
#include "helper.h"
#include "flee.h"

Collect::Collect(Fruit *idP, unsigned int x0, unsigned int y0, int val): Task(T_COLLECT, idP, 0, x0, y0, val) {

}

bool Collect::execute() {

    float valeur=0.5;
    float danger=1.5;

        if(idParent->sugarWallet == idParent->sugar || idParent->vitaminsWallet == idParent->vitamins)
        {
                delete idParent->task;
                idParent->task = new Flee(idParent);
                return false;
        }



    path.clear();
    pf.start = idParent->position;
    pf.end = getFreeSpaceAround(&ai.world.at(x,y));

            if(value == SUGAR && idParent->pickUpSugar(getSugarDropId(x,y))==OK) return true;
            else if(value == VIT) {
                int ret = idParent->drawVitamin();
                cout << "Draw vit" << ret << endl;//"//ai.api->decode(ret) << endl;
                if (ret == OK) return true;
                if (ret == VITAMINS_WALLET_FULL) {
                    delete idParent->task;
                    idParent->task = new Flee(idParent);
                    return false;
                }
            }
            //else
            if(!pf.compute(&path,danger,valeur)) return false;
            cout << "lonchem" << path.size();
            list<map_chunk*>::iterator it = path.begin();
            for( int i = 0; i < idParent->speed; i++ ) if( it != path.end() )it++;
                if( it == path.end() ) {it--;}
                //if( (*it) == pf.end ) it--;
            cout << "coord: " << (*it)->X_coord << "  "<< (*it)->Y_coord <<endl;
            switch (idParent->move((*it)->X_coord, (*it)->Y_coord )){
            case UNKNOWN_OBJECT: cout << "unknown object"; break;
            case NOT_FRUIT: cout << "not fruit"; break;
            case NOT_OWNER: cout << "not owner"; break;
            case NOT_VALID_DESTINATION: cout << "destination invalide"; break;
            case NO_MORE_ACTIONS: cout << "no more actions"; break;
            case NOT_VALID_POSITION : cout << "position invalide"; break;
            case TOO_FAR: cout << "Trop loin"; break;
            case OK: cout << "OK"; break;
        }

    idParent->move(x,y);

    return true;
}


